from visual import *
from constants import sphereDistance, sin60, numberOfLines
from ballGenerator import getNewBall


def getBallPositionAddLayer(i, j, k):
    x = (k - 0.5 * (numberOfLines - (i + j) % 2)) * sphereDistance
    y = (j - 0.5 * (numberOfLines - i % 2)) * sphereDistance * sin60
    z = 0
    return vector(x, y, z)
#izračun pozicija kugli za novi sloj
    
def addLayer (allBalls, turnAddLayer):
    j = 0 
    while j < numberOfLines:
        k = 0
        while k < numberOfLines:
            rate(100)
            position = getBallPositionAddLayer(turnAddLayer,j,k)
            if position:
                newBall = getNewBall()
                newBall.pos = position
                newBall.opacity = 1
                allBalls.append(newBall)
            k = k + 1
        j = j + 1
#dodaje nove kugle

def moveBallsZ (allBalls, emptyPlaces, turnAddLayer):
    deleteEmptyPlacesIndex = []
    print("pomicanje")
    for index in range(len(allBalls)):
        allBalls[index].pos.z = allBalls[index].pos.z + sphereDistance * sin60
    addLayer (allBalls, turnAddLayer)
    for index2 in range(len(emptyPlaces)):
        emptyPlaces[index2].z = emptyPlaces[index2].z + sphereDistance * sin60
        if (emptyPlaces[index2].z > 8.5):
            #ako je slobodna pozicija previše blizu topu briše se
            deleteEmptyPlacesIndex.append(index2)
    for index3 in sorted(deleteEmptyPlacesIndex, reverse = True):
        del emptyPlaces[index3]                  
#pomiče postojeće kugle  i lokacije praznih mjesta
